package TransactionImplementation

import (
	"PaySalary/DataBase"
	"PaySalary/ModelImplementation"
	"PaySalary/TransactionApplication"
	"fmt"
	"log"
	"reflect"
	"strconv"
	"time"
)

type TimeCardTransaction struct {
	TransactionApplication.Transaction
	itsDate time.Time
	itsHours float64
	itsEmployeeId int
}

func (t *TimeCardTransaction) NewTimeCardTransaction(date time.Time, hours float64, empid int) {
	t.itsDate = date
	t.itsEmployeeId = empid
	t.itsHours = hours
}

func (t *TimeCardTransaction) Execute() {
	e := DataBase.PayrollDatabase.GetEmployee(t, t.itsEmployeeId)

	if e != nil {
		pc := e.GetClassification()
		if reflect.TypeOf(pc) == reflect.TypeOf(ModelImplementation.HourlyClassification{}) {
			hc := pc.(*ModelImplementation.HourlyClassification)
			hc.AddTimeCard(ModelImplementation.NewTimeCard(t.itsDate,t.itsHours))
		} else {
			log.Println(fmt.Errorf(DataBase.PayrollExceptionMessage.NotHourlyClassification + strconv.Itoa(e.GetEmployeeId())))
		}
	} else {
		log.Println(fmt.Errorf(DataBase.PayrollExceptionMessage.EmployeeNotExist + strconv.Itoa(e.GetEmployeeId())))
	}
}